from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE TABLE IF NOT EXISTS "user" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "username" VARCHAR(50) NOT NULL UNIQUE,
    "hashed_password" VARCHAR(128) NOT NULL,
    "nickname" VARCHAR(50)
);
CREATE TABLE IF NOT EXISTS "tb_blog_categories" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "name" VARCHAR(255) NOT NULL,
    "short_name" VARCHAR(255) NOT NULL,
    "parent_id" UUID REFERENCES "tb_blog_categories" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_tb_blog_cat_name_ed4643" ON "tb_blog_categories" ("name");
CREATE INDEX IF NOT EXISTS "idx_tb_blog_cat_short_n_28f4ad" ON "tb_blog_categories" ("short_name");
CREATE TABLE IF NOT EXISTS "tb_blog_articles" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "title" VARCHAR(255) NOT NULL,
    "content" TEXT NOT NULL,
    "cover" VARCHAR(255) NOT NULL,
    "cover_hash" VARCHAR(255),
    "top" BOOL NOT NULL,
    "status" VARCHAR(255) NOT NULL,
    "views" INT NOT NULL,
    "keywords" VARCHAR(255),
    "description" VARCHAR(255),
    "password" VARCHAR(255),
    "category_id" UUID NOT NULL REFERENCES "tb_blog_categories" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_tb_blog_art_title_d49207" ON "tb_blog_articles" ("title");
CREATE TABLE IF NOT EXISTS "tb_blog_tags" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "name" VARCHAR(255) NOT NULL
);
CREATE INDEX IF NOT EXISTS "idx_tb_blog_tag_name_6c5489" ON "tb_blog_tags" ("name");
CREATE TABLE IF NOT EXISTS "tb_blog_thoughts" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "content" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "tb_gallery_category" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "name" VARCHAR(10) NOT NULL
);
COMMENT ON COLUMN "tb_gallery_category"."name" IS '相册分类名称';
CREATE TABLE IF NOT EXISTS "tb_gallery" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "title" VARCHAR(50) NOT NULL,
    "content" VARCHAR(255),
    "image" VARCHAR(255) NOT NULL,
    "image_hash" VARCHAR(32) NOT NULL,
    "category_id" UUID NOT NULL REFERENCES "tb_gallery_category" ("id") ON DELETE CASCADE
);
COMMENT ON COLUMN "tb_gallery"."title" IS '相册标题';
COMMENT ON COLUMN "tb_gallery"."content" IS '相册内容';
COMMENT ON COLUMN "tb_gallery"."image" IS '相册封面';
COMMENT ON COLUMN "tb_gallery"."image_hash" IS '相册封面hash';
COMMENT ON COLUMN "tb_gallery"."category_id" IS '相册分类';
CREATE TABLE IF NOT EXISTS "tb_fileserver_files" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "filename" VARCHAR(255) NOT NULL,
    "filesize" BIGINT   DEFAULT 0,
    "file_url" VARCHAR(255) NOT NULL,
    "file_hash" VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS "tb_messages" (
    "id" UUID NOT NULL  PRIMARY KEY,
    "active" BOOL   DEFAULT True,
    "created_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ   DEFAULT CURRENT_TIMESTAMP,
    "qq" VARCHAR(20) NOT NULL,
    "nickname" VARCHAR(20) NOT NULL,
    "website" VARCHAR(100),
    "content" TEXT NOT NULL,
    "browser" VARCHAR(7) NOT NULL  DEFAULT 'UNKNOWN',
    "system" VARCHAR(7) NOT NULL  DEFAULT 'UNKNOWN',
    "ip" VARCHAR(39)
);
CREATE INDEX IF NOT EXISTS "idx_tb_messages_qq_dc2807" ON "tb_messages" ("qq");
COMMENT ON COLUMN "tb_messages"."qq" IS 'QQ号';
COMMENT ON COLUMN "tb_messages"."nickname" IS '昵称';
COMMENT ON COLUMN "tb_messages"."website" IS '个人网站';
COMMENT ON COLUMN "tb_messages"."content" IS '留言内容';
COMMENT ON COLUMN "tb_messages"."browser" IS '浏览器';
COMMENT ON COLUMN "tb_messages"."system" IS '操作系统';
COMMENT ON COLUMN "tb_messages"."ip" IS 'IP地址';
CREATE TABLE IF NOT EXISTS "aerich" (
    "id" SERIAL NOT NULL PRIMARY KEY,
    "version" VARCHAR(255) NOT NULL,
    "app" VARCHAR(100) NOT NULL,
    "content" JSONB NOT NULL
);
CREATE TABLE IF NOT EXISTS "tb_blog_articles_tb_blog_tags" (
    "tb_blog_articles_id" UUID NOT NULL REFERENCES "tb_blog_articles" ("id") ON DELETE CASCADE,
    "tag_id" UUID NOT NULL REFERENCES "tb_blog_tags" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS "uidx_tb_blog_art_tb_blog_476b2b" ON "tb_blog_articles_tb_blog_tags" ("tb_blog_articles_id", "tag_id");"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        """
